//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.me.salesperson;

import java.math.BigDecimal;
import net.sf.jasperreports.engine.*;
import org.jpgolay.me.item.Item;
import org.jpgolay.me.persistence.ObjectCRUD;
import org.jpgolay.me.util.Parameter;

// Referenced classes of package org.jpgolay.me.salesperson:
//            SalesPerson
public class SalesPersonPrintDataSource
        implements JRDataSource {

    public SalesPersonPrintDataSource(SalesPerson salesPerson, Object bills[][], Object familyCommission[][], Object itemCommission[][], Parameter param) {
        this.bills = bills;
        this.salesPerson = salesPerson;
        this.familyCommission = familyCommission;
        this.itemCommission = itemCommission;
        this.param = param;
        countLineFamily = 0;
        countLineItem = 0;
        count = 27 * (familyCommission.length + itemCommission.length);
        crud = new ObjectCRUD();
    }

    public boolean next()
            throws JRException {
        return count > 0;
    }

    public Object getFieldValue(JRField field)
            throws JRException {
        String fieldname = field.getName();
        String value = null;
        if ("Id".equals(fieldname)) {
            value = salesPerson.getId().toString();
        } else if ("Firstname".equals(fieldname)) {
            value = salesPerson.getPrenom();
        } else if ("Lastname".equals(fieldname)) {
            value = salesPerson.getNom();
        } else if ("AddrLn1".equals(fieldname)) {
            value = salesPerson.getAddrLn1();
        } else if ("AddrLn2".equals(fieldname)) {
            value = salesPerson.getAddrLn2();
        } else if ("Zip".equals(fieldname)) {
            value = salesPerson.getZip();
        } else if ("City".equals(fieldname)) {
            value = salesPerson.getCity();
        } else if ("Country".equals(fieldname)) {
            value = salesPerson.getCountry();
        } else if ("Tel".equals(fieldname)) {
            value = salesPerson.getTelephone();
        } else if ("Fax".equals(fieldname)) {
            value = salesPerson.getFax();
        } else if ("Cel".equals(fieldname)) {
            value = salesPerson.getPortable();
        } else if ("Email".equals(fieldname)) {
            value = salesPerson.getEmail();
        } else if ("Date".equals(fieldname)) {
            String date = salesPerson.getUpdateLastTime().substring(6, 8) + "/" + salesPerson.getUpdateLastTime().substring(4, 6) + "/" + salesPerson.getUpdateLastTime().substring(0, 4) + " " + salesPerson.getUpdateLastTime().substring(8, 10) + ":" + salesPerson.getUpdateLastTime().substring(10, 12) + ":" + salesPerson.getUpdateLastTime().substring(12);
            value = date;
        } else if ("Family".equals(fieldname)) {
            value = salesPerson.getSalesPersonFamily();
        } else if ("Level".equals(fieldname)) {
            value = salesPerson.getNiveau();
        } else if ("Sector".equals(fieldname)) {
            value = salesPerson.getSecteur();
        } else if ("Notes".equals(fieldname)) {
            value = salesPerson.getCommentaire();
        } else if ("Goal".equals(fieldname)) {
            value = salesPerson.getObjectif().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
        } else if ("Result".equals(fieldname)) {
            value = salesPerson.getResultat().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
        } else if ("Rate1".equals(fieldname)) {
            value = salesPerson.getTauxVente().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
        } else if ("Rate2".equals(fieldname)) {
            value = salesPerson.getTauxTotalVente().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
        } else if ("GoalDate".equals(fieldname)) {
            value = salesPerson.getObjectifDate();
        } else if ("ResultDate".equals(fieldname)) {
            value = salesPerson.getResultatDate();
        } else if ("Reference".equals(fieldname)) {
            value = commissiontList(0);
        } else if ("Rate".equals(fieldname)) {
            value = commissiontList(1);
        } else if ("From".equals(fieldname)) {
            value = commissiontList(2);
        } else if ("To".equals(fieldname)) {
            value = commissiontList(3);
        }
        count--;
        return value;
    }

    private String commissiontList(int col) {
        if (countLineItem < itemCommission.length) {
            switch (col) {
                case 0: // '\0'
                    if (itemCommission[countLineItem][col] != null) {
                        return "A - " + (String) itemCommission[countLineItem][col] + " " + getItemDesignation((String) itemCommission[countLineItem][col]);
                    } else {
                        return null;
                    }

                case 1: // '\001'
                    if (itemCommission[countLineItem][col] != null) {
                        return ((BigDecimal) itemCommission[countLineItem][col]).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
                    } else {
                        return null;
                    }

                case 2: // '\002'
                    if (itemCommission[countLineItem][col] != null) {
                        return (String) itemCommission[countLineItem][col];
                    } else {
                        return null;
                    }

                case 3: // '\003'
                    countLineItem++;
                    if (itemCommission[countLineItem - 1][col] != null) {
                        return (String) itemCommission[countLineItem - 1][col];
                    } else {
                        return null;
                    }
            }
            return null;
        }
        if (countLineFamily < familyCommission.length) {
            switch (col) {
                case 0: // '\0'
                    if (familyCommission[countLineFamily][col] != null) {
                        return "F - " + (String) familyCommission[countLineFamily][col];
                    } else {
                        return null;
                    }

                case 1: // '\001'
                    if (familyCommission[countLineFamily][col] != null) {
                        return ((BigDecimal) familyCommission[countLineFamily][col]).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
                    } else {
                        return null;
                    }

                case 2: // '\002'
                    if (familyCommission[countLineFamily][col] != null) {
                        return (String) familyCommission[countLineFamily][col];
                    } else {
                        return null;
                    }

                case 3: // '\003'
                    countLineFamily++;
                    if (familyCommission[countLineFamily - 1][col] != null) {
                        return (String) familyCommission[countLineFamily - 1][col];
                    } else {
                        return null;
                    }
            }
            return null;
        } else {
            return null;
        }
    }

    private String getItemDesignation(String itemId) {
        Item item = (Item) crud.read(itemId, 11);
        if (item != null) {
            return item.getDesignation();
        } else {
            return "";
        }
    }
    private SalesPerson salesPerson;
    private Object familyCommission[][];
    private Object itemCommission[][];
    private Object bills[][];
    private Parameter param;
    private int count;
    private int countLineFamily;
    private int countLineItem;
    private ObjectCRUD crud;
}
